#ifndef _MERGE_H_
#define _MERGE_H_

void mergeold (int* V1, int* V2, int* temp,int size1,int size2)
{
    int i=0, j=0, k=0;
    while (i < size1 && j < size2)
    {
        if (V1[i] < V2[j])
        {
            temp[k] = V1[i];
            i++;
            k++;

        }
        else if (V1[i] > V2[j])
        {
            temp[k] = V2[j];
            j++;
            k++;

        }
        else if (V1[i] == V2[j])
        {
            temp[k] = V1[i];
            i++;
            k++;

        }
    }
    if(i<size1)
        while (i<size1)
        {
            temp[k] = V1[i];
            i++;
            k++;
        }
    if(j<size2)
        while (j<size2)
        {
            temp[k] = V2[j];
            j++;
            k++;
        }
}

void merge(int *a, int m, int *b, int n, int *sorted) {
  int i, j, k;

  j = k = 0;

  for (i = 0; i < m + n;) {
    if (j < m && k < n) {
      if (a[j] < b[k]) {
        sorted[i] = a[j];
        j++;
      }
      else {
        sorted[i] = b[k];
        k++;
      }
      i++;
    }
    else if (j == m) {
      for (; i < m + n;) {
        sorted[i] = b[k];
        k++;
        i++;
      }
    }
    else {
      for (; i < m + n;) {
        sorted[i] = a[j];
        j++;
        i++;
      }
    }
  }
}

#endif
